package com.gmcc.docmanager;

import android.app.ActivityGroup;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class HeaderActivity extends ActivityGroup {
	protected View mHeaderContainer;
	protected Button mButtonGoback, mButtonLeft, mButtonRight;
	protected TextView mTitle;
	
	protected RelativeLayout normalLayout;
	protected LinearLayout tabLayout;
	private void initHeader() {
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.layout_header);

		mHeaderContainer = findViewById(R.id.layout_header);
		ViewGroup parent = (ViewGroup) mHeaderContainer.getParent();
		if (parent == null)
			return;
		ViewGroup.LayoutParams lp = parent.getLayoutParams();
		if (lp == null) {
			lp = new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.WRAP_CONTENT);
		} else {
			lp.height = LayoutParams.WRAP_CONTENT;
			lp.width = LayoutParams.FILL_PARENT;
		}
		parent.setLayoutParams(lp);
		//parent.setBackgroundResource(R.drawable.header_bg);
		parent.setPadding(0, 0, 0, 0);
		
		normalLayout = (RelativeLayout)mHeaderContainer.findViewById(R.id.layout_header_normal);
		tabLayout = (LinearLayout)mHeaderContainer.findViewById(R.id.layout_header_tab);
		mButtonGoback = (Button) mHeaderContainer.findViewById(R.id.layout_header_back);
		mButtonLeft = (Button) mHeaderContainer.findViewById(R.id.layout_header_tab_left);
		mButtonRight = (Button) mHeaderContainer.findViewById(R.id.layout_header_tab_right);
		mTitle = (TextView) mHeaderContainer.findViewById(R.id.tvTitle);

		mButtonGoback.setOnClickListener(mOnClickListener);
	}
	
	protected void showNormalHeader(){
		if(normalLayout != null)
			normalLayout.setVisibility(View.VISIBLE);
		if(tabLayout != null){
			tabLayout.setVisibility(View.GONE);
		}
	}
	
	protected void showTabHeader(String leftString, String rightString){
		if(tabLayout != null)
			tabLayout.setVisibility(View.VISIBLE);
		if(normalLayout != null)
			normalLayout.setVisibility(View.GONE);
		if(mButtonLeft != null && mButtonRight != null){
			mButtonLeft.setText(leftString);
			mButtonRight.setText(rightString);
		}
	}
	
	public void hideBackButton(){
		if(mButtonGoback!=null)
			mButtonGoback.setVisibility(View.GONE);
	}
	
	public void showBackButton(){
		if(mButtonGoback!=null)
			mButtonGoback.setVisibility(View.GONE);
	}

	@Override
	public void setContentView(int layoutResID) {
		View view=getLayoutInflater().inflate(layoutResID, null);
		this.setContentView(view);
	}

	@Override
	public void setContentView(View view) {
		super.setContentView(view);
		initHeader();
		view.setBackgroundResource(R.drawable.body_bg);
		this.setTitle(getTitle());
	}

	@Override
	public void setContentView(View view, LayoutParams params) {
		super.setContentView(view, params);
		initHeader();
		view.setBackgroundResource(R.drawable.body_bg);
	}
	
	@Override
	public void setTitle(CharSequence title) {
		if (mTitle != null) {
			mTitle.setText(title);
		}
		super.setTitle(title);
	}

	@Override
	public void setTitle(int titleId) {
		if (mTitle != null) {
			mTitle.setText(titleId);
		}
		super.setTitle(titleId);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
	}

	protected void onGobackClick(View v) {
		finish();
	}

	private View.OnClickListener mOnClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.layout_header_back :
					onGobackClick(v);
					break;
			}
		}
	};
}// end class